<!-- 瀑布流布局 -->
<template>
	<view class="container">
		<view class="cont-box" :style="{ '--layout-width': 100 / flowData.column - flowData.columnSpace + '%' }"
			v-for="(numVal, index) in flowData.column" :key="numVal">
			<view @click.stop="wapperClick(item)" class="item-box" v-for="(item, j) in flowData[`column_${index + 1}`]" :key="j">
				<image :src="item.image" mode="widthFix" :class="['img-tip',{'img-hide':item.image==null}]" lazy-load>
					<view class="f0">
						<view :class="['title', { notitle: item.footPrintFiles.length==0 }]">
							{{item.scenicCityName}}
						</view>
						<view :class="['time', { notime: item.footPrintFiles.length==0 }]">
							{{getYearMonth(item.unLockTime)}}
						</view>
						<view v-if="item.footPrintFiles.length==0" class="editbtn">编辑内容</view>
					</view>
				</image>
				<view class="item">

					<view v-if="item.footPrintFiles.length>0" class="f1">
						<view class="left">
							<image src="../../static/icon/img.png" class="img"></image>
							<view class="num">{{getcout(false,item.footPrintFiles)}}</view>
						</view>
						<view class="right">
							<image src="../../static/icon/see.png" class="img"></image>
							<view class="num">{{getcout(true,item.footPrintFiles)}}</view>
						</view>
					</view>
					<view class="f2">第{{item.rank}}位解锁人</view>
					<view class="f3">打卡时间：{{formatDate(item.unLockTime)}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			value: Array,
			column: { // 列的数量 
				type: [String, Number],
				default: 2
			},

			columnSpace: { // 列之间的间距 百分比
				type: [String, Number],
				default: 2
			}
		},
		data() {
			return {
				flowData: {
					list: this.value ? this.value : [],
					column: this.column < 2 ? 2 : this.column,
					columnSpace: this.columnSpace <= 5 ? this.columnSpace : 5,
				}
			};
		},
		created() {
			
			/* 初始化每一列的数据 */
			for (let i = 1; i <= this.flowData.column; i++) {
				this.$set(this.flowData, `column_${i}`, []);
			}
			this.$nextTick(() => {
				this.initData(); // 数据初始化
			});
		},

		watch: {
			value: {
				deep: true,
				handler(newValue, oldValue) {
					setTimeout(() => {
						this.$nextTick(() => {
							this.flowData.list=newValue
							/* 初始化每一列的数据 */
							for (let i = 1; i <= this.flowData.column; i++) {
								this.$set(this.flowData, `column_${i}`, []);
							}
							
								this.initData(); // 数据初始化
							
						});
					}, 10)
				}
			}
		},
		methods: {
			// 单项点击事件
			wapperClick(item) {
				this.$emit('wapperClick', item);
			},
			// 补齐日期数字前缀为两位数字
			padStart(num) {
				return num.toString().padStart(2, '0');
			},
			// 将日期格式化为 年.月.日 的形式
			formatDate(dateString) {
				const date = new Date(dateString);
				const year = date.getFullYear();
				const month = this.padStart(date.getMonth() + 1); // 月份是从0开始的
				const day = this.padStart(date.getDate());
				return `${year}.${month}.${day}`;
			},

			// 将日期格式化为 年.月 的形式
			getYearMonth(dateString) {
				const date = new Date(dateString);
				const year = date.getFullYear();
				const month = this.padStart(date.getMonth() + 1);
				return `${year}.${month}`;
			},
			getcout(type, list) {
				if (list) {
					return list.filter(item => item.type === type).length
				} else {
					return 0
				}
			},


			/* 数据初始化 */
			initData() {
				const groupList = this.dynamicGrouping(this.flowData.list, this.flowData.column); // 数据动态分组
				groupList.forEach((item, i) => (this.flowData[`column_${i + 1}`] = item));
			},
			/** 数据动态分组
			 * @param {Object} data 分组的数据列表
			 * @param {Object} i 需要分几组
			 * @returns {Array} groups 已分组的数据
			 */
			dynamicGrouping(data, i) {
				if (i <= 0) return "分组数必须大于0";
				const groups = [];
				for (let j = 0; j < i; j++) {
					groups.push([]);
				}
				for (let k = 0; k < data.length; k++) {
					const groupIndex = k % i;
					groups[groupIndex].push(data[k]);
				}
				return groups;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		justify-content: space-between;
		padding: 20rpx;
		$borderRadius: 12rpx;

		.cont-box {
			width: var(--layout-width);

			.item-box {
				background-color: #fff;
				width: 100%;
				padding-bottom: 20rpx;
				margin-bottom: 30rpx;
				border-radius: $borderRadius;
				box-shadow: 0rpx 3rpx 6rpx rgba(0, 46, 37, 0.08);

				.img-tip {
					width: 100%;
					border-radius: $borderRadius $borderRadius 0 0;
					position: relative;
					background: #f2e6d9;
				}

				.img-hide {
					height: 220rpx;
					background-color: #f2e6d9;
				}

				.tit-tip {
					text-align: justify;
					font-size: 30rpx;
					padding: 10rpx 20rpx 0;
					font-weight: 900;
				}

				.desc-tip {
					text-align: justify;
					font-size: 26rpx;
					padding: 5rpx 20rpx 0;
					margin-top: 10rpx;
				}
			}
		}
	}

	/* 多行省略 */
	.multi-line-omit {
		word-break: break-all; // 允许单词内自动换行，如果一个单词很长的话
		text-overflow: ellipsis; // 溢出用省略号显示
		overflow: hidden; // 超出的文本隐藏
		display: -webkit-box; // 作为弹性伸缩盒子模型显示
		-webkit-line-clamp: 2; // 显示的行
		-webkit-box-orient: vertical; // 设置伸缩盒子的子元素排列方式--从上到下垂直排列
	}

	/* 单行省略 */
	.one-line-omit {
		width: 100%; // 宽度100%：1vw等于视口宽度的1%；1vh等于视口高度的1%
		white-space: nowrap; // 溢出不换行
		overflow: hidden; // 超出的文本隐藏
		text-overflow: ellipsis; // 溢出用省略号显示
	}

	.f0 {
		    position: absolute;
		    top: 24rpx;
		    left: 24rpx;
		
		.title {
			font-size: 40rpx;
			font-family: PingFang SC, PingFang SC-Semibold;
			font-weight: Semibold;
			text-align: left;
			color: #ffffff;
			line-height: 56rpx;
			line-height: 56rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			width: 320rpx;
		}

		.time {
			margin-top: 4rpx;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC-Medium;
			font-weight: Medium;
			text-align: left;
			color: #ffffff;
			line-height: 40rpx;
		}
	}

	.editbtn {
		width: 160rpx;
		height: 48rpx;
		background: #1f1f1f;
		border-radius: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 30rpx;
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: Regular;
		color: #ffffff;
		line-height: 40rpx;
	}

	.notitle {
		color: #1F1F1F !important;
	}

	.notime {
		color: #4B4B4B !important;
	}

	.f1 {
		padding-left: 19rpx;
		box-sizing: border-box;
		margin-top: 12rpx;
		display: flex;
		align-items: center;

		.left {
			display: flex;
			align-items: center;
		}

		.img {
			width: 32rpx;
			height: 32rpx;
		}

		.num {
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC-Regular;
			font-weight: Regular;
			text-align: left;
			color: #333333;
			line-height: 40rpx;
			margin-left: 6rpx;
		}

		.right {
			display: flex;
			align-items: center;
			margin-left: 40rpx;
		}
	}

	.f2 {
		padding-left: 19rpx;
		box-sizing: border-box;
		margin-top: 20rpx;
		font-size: 24rpx;
		font-family: PingFang SC, PingFang SC-Medium;
		font-weight: Medium;
		text-align: left;
		color: #333333;
		line-height: 34rpx;
	}

	.f3 {
		padding-left: 19rpx;
		box-sizing: border-box;
		margin-top: 8rpx;
		font-size: 24rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: Regular;
		text-align: left;
		color: #4b4b4b;
		line-height: 34rpx;
		margin-bottom: 24rpx;
	}
</style>